#include <sys/io.h> 
#include <unistd.h> 
#include <stdio.h> 
#include <stdlib.h>
#include <string.h>

#include "win_menu.h"

#include <ncurses.h>
#include <menu.h>

#include "ec_read_write.h"

#if 0 
void dump_code(unsigned char addr_port, unsigned char data_port, unsigned short offset, unsigned short length) 
{ 
	unsigned char i, *data; 
	data = (unsigned char*)malloc(length); 
	if (!data) 
	{ 
		perror("Allocation memory failed!\n"); exit(1); 
	} 
	for (i = offset; i < offset + length; i++) 
	{ 
		outb(i, addr_port); 
		data[i - offset] = inb(data_port); 
	} 
	for (i = 0; i < length; i++) 
		printf("%02x ", data[i]); 
	printf("\n"); free(data); 
} 

//int main(int argc, char *argv[]) 
int dump_test(void) 
{ 
	if (iopl(3) < 0) 
	{ 
		perror("iopl access error!\n"); 
		exit(1); 
	} 
	while (1) 
	{ 
		dump_code(0x70, 0x71, 0x00, 10); 
		sleep(1); 
	}
}
#endif

#if 0
int dump_test(void)
{
	if (iopl(3) < 0) 
	{ 
		perror("iopl access error!\n"); 
		exit(1); 
	}

	outb(0x07,0x2E);
	outb(0x11,0x2F);

	
	printf("9999\n    ");
	for(int i=0;i<16;i++)
		printf("%02X ",i);
	//printf("\n");

	for(int i=0;i<256;i++)
	{
		if(i%16 == 0)
		{
			printf("\n%02Xh ",i);
		}

		outb(i,0x2E);
		int data=inb(0x2F);
		printf("%02X ",data);

	}

	printf("\n");
}
#endif

int rtc_read_byte(int nbaseaddr)
{
	printf("    fggg11\n");
	iopl(3);
	for(int Index=0;Index<0x0E;Index++)
	{

	
	outb(Index,nbaseaddr);
	int data=inb(nbaseaddr+1);
	printf("%02X ", data);


	}
	printf("    fggg22\n");
	


}


int pnp_read_byte(int Index)
{

	//mvprintw(LINES - 4, 0, "You have chosen addr %08Xh ", 	Index);


	if (iopl(3) < 0) 
	{
		#if 0
		wattron(stdscr, color);
		mvprintw(LINES - 2, 0, "iopl access error! run as root, sudo ./target/bin/test_tool_BM6J58 \n"); 
		mvprintw(LINES - 3, 0, "You have chosen %d ", 	Index);
		wattroff(stdscr, color);
		#endif
		printf( "iopl(3) err ");


		return -1;
		//exit(1); 
	}

	outb(0x07,0x2E);
	outb(Index >> 8,0x2F);

	
	outb(Index&0xFF,0x2E);
	int data=inb(0x2F);

}

int pnp_write_byte(int Index,int data)
{

	//mvprintw(LINES - 4, 0, "You have chosen addr %08Xh ", 	Index);


	if (iopl(3) < 0) 
	{
		printf( "iopl(3) err ");



		return -1;
		//exit(1); 
	}

	outb(0x07,0x2E);
	outb(Index >> 8,0x2F);

	
	outb(Index&0xFF,0x2E);

	outb(data,0x2F);
	return 0x00;

}


int read_PNPCFG(int nbase_addrpara,struct_cell_data* p_type_cell_data, chtype color)
{

	//mvprintw(LINES - 4, 0, "You have chosen addr %08Xh ", 	nbase_addrpara);


	if (iopl(3) < 0) 
	{
		#if 1
		wattron(stdscr, color);
		mvprintw(LINES - 2, 0, "iopl access error! run as root, sudo ./target/bin/test_tool_BM6J58 \n"); 
		mvprintw(LINES - 3, 0, "You have chosen %d ", 	nbase_addrpara);
		wattroff(stdscr, color);
		#endif



		return -1;
		//exit(1); 
	}

	outb(0x07,0x2E);
	outb(nbase_addrpara,0x2F);

	
	for(int i=0;i<256;i++)
	{
		outb(i,0x2E);
		int data=inb(0x2F);
		char str[25];
		//itoa(i, str, 16);
		sprintf(str,"%02X ", data );
		//sprintf(str,"%02X ",nVal);
		//mvprintw(LINES - 8+ i/32, i%32*3, "%02X ", 	data);
		
		memcpy(p_type_cell_data->data_cell[i/const_nMaxCell][i%const_nMaxCell],str,2);
	
	}
	return 0;
}


int read_EC_RAM(int addr)
{

	//mvprintw(LINES - 4, 0, "You have chosen addr %d ", 	addr);
	//printf( "You have chosen addr %08Xh ", 	addr);

	if (iopl(3) < 0) 
	{
		#if 0
		wattron(stdscr, color);
		mvprintw(LINES - 2, 0, "iopl access error! run as root, sudo ./target/bin/test_tool_BM6J58 \n"); 
		mvprintw(LINES - 3, 0, "You have chosen %d ", 	addr);
		wattroff(stdscr, color);
		#endif
		printf( "iopl(3) err ");

		return -1;
		//exit(1); 
	}


	unsigned char it8528_read(unsigned char index);
	for(int index=0; index<256;index++)
	{
		if(index%16==0)
			printf("\n");
		unsigned char nval=it8528_read(index);
		printf("%02X ", nval);		
	}
	printf("\n");


	return 0;
}



int ecio_read_byte(unsigned short Index)
{
		if (iopl(3) < 0) 
	{
		#if 0
		wattron(stdscr, color);
		mvprintw(LINES - 2, 0, "iopl access error! run as root, sudo ./target/bin/test_tool_BM6J58 \n"); 
		mvprintw(LINES - 3, 0, "You have chosen %d ", 	addr);
		wattroff(stdscr, color);
		#endif
		printf( "iopl(3) err ");

		return -1;
		//exit(1); 
	}

  int ret = 0xff; 

  outb(0x2e,0x2e);
  outb(0x11,0x2f);
  outb(0x2f,0x2e);
  outb(Index >> 8,0x2f);

  outb(0x2e,0x2e);
  outb(0x10,0x2f);
  outb(0x2f,0x2e);
  outb(Index&0xFF,0x2f);

  outb(0x2e,0x2e);
  outb(0x12,0x2f);
  outb(0x2f,0x2e);
  ret = inb(0x2f);

  //printf("ret is 0x%x!\n", ret);
  return ret;
}


int ecio_write_byte(unsigned short Index,int data)
{

	if (iopl(3) < 0) 
	{
		#if 0
		wattron(stdscr, color);
		mvprintw(LINES - 2, 0, "iopl access error! run as root, sudo ./target/bin/test_tool_BM6J58 \n"); 
		mvprintw(LINES - 3, 0, "You have chosen %d ", 	addr);
		wattroff(stdscr, color);
		#endif
		printf( "iopl(3) err ");

		return -1;
		//exit(1); 
	}

  int ret = 0xff; 

  outb(0x2e,0x2e);
  outb(0x11,0x2f);
  outb(0x2f,0x2e);
  outb((Index >> 8)&0xFF,0x2f);

  outb(0x2e,0x2e);
  outb(0x10,0x2f);
  outb(0x2f,0x2e);
  outb(Index&0xFF,0x2f);

  outb(0x2e,0x2e);
  outb(0x12,0x2f);
  outb(0x2f,0x2e);
  outb(data,0x2f);

//  printk("ret is 0x%x!\n", ret);
  return ret;
}


int read_ECIO_RAM(int nbase_addrpara,struct_cell_data* p_type_cell_data, chtype color)
{

	//mvprintw(LINES - 4, 0, "You have chosen addr %08Xh ", 	nbase_addrpara);
	//printf( "You have chosen addr %d ", 	nbase_addr);

	if (iopl(3) < 0) 
	{
		#if 1
		wattron(stdscr, color);
		mvprintw(LINES - 2, 0, "iopl access error! run as root, sudo ./target/bin/test_tool_BM6J58 \n"); 
		mvprintw(LINES - 3, 0, "You have chosen %d ", 	nbase_addrpara);
		wattroff(stdscr, color);
		#endif
		//printf( "iopl(3) err ");

		return -1;
		//exit(1); 
	}


	//unsigned char ecio_read(unsigned char index);
	unsigned short nbaseaddr=(unsigned short)nbase_addrpara;



	for(int index=0; index<256;index++)
	{
		//if(index%16==0)
		//	printf("\n");
		unsigned char data=ecio_read_byte(nbaseaddr+index);


			char str[25];
		//itoa(i, str, 16);
		sprintf(str,"%02X ", data );
		//sprintf(str,"%02X ",nVal);
		//mvprintw(LINES - 8+ i/32, i%32*3, "%02X ", 	data);
		
		memcpy(p_type_cell_data->data_cell[index/const_nMaxCell][index%const_nMaxCell],str,2);
			
		//printf("%02X ", nval);	



	}
	//printf("\n");


	return 0;
}